<?php

namespace addons\TfFutures\api\controllers;

use addons\TfFutures\AddonConfig;
use addons\TfFutures\common\models\Member;
use common\models\common\Capital;
use Yii;
use common\helpers\ResultDataHelper;
use api\controllers\OnAuthController;
use trade\common\service\KlineService;


class GetKlineController extends OnAuthController
{
    public $modelClass = Capital::class;

    /**
     * 不用进行登录验证的方法
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     * @var array
     */
    protected $optional = ['get-kline', 'get-home', 'get-line','app-kline'];

    public function actionGetHome()
    {
        return ResultDataHelper::api(200, "home data",
            json_decode(file_get_contents(dirname(dirname(dirname(__FILE__))) . '/home.json')));
    }

    public static function getPrex()
    {
        $pre = (new AddonConfig())->stockpre;

        return $pre;
    }
    //app其他k线
    public function actionAppKline(){
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(403, "access done!");
        }
        $symbolcode = Yii::$app->request->get('symbol');
        if (empty($symbolcode)) {
            return ResultDataHelper::api(422, "产品code不能为空");
        }
        $type = Yii::$app->request->get('range');$date = 'Y-m-d H:i';
        switch ($type) {
            case 'year'://年线
                $list = 'year';
                $date = 'Y';
                break;
            case 'month'://月线
                $list = 'month';
                $date = 'Y-m';
                break;
            case '604800000'://周线
                $list = 'week';
                $date = 'Y-m-d';
                break;
            case '86400000'://日线
                $list = 'day';
                $date = 'Y-m-d';
                break;
            case '14400000'://4小时
                $list = 240;
                $date = 'Y-m-d H';
                break;
            case '3600000'://一小时
                $list = 60;
                break;
            case '180000'://30分钟
                $list = 30;
                break;
            case '90000'://15分钟
                $list = 15;
                break;
            case '30000'://5分钟
                $list = 5;
                break;
            case '6000'://5分钟
                $list = 1;
                break;
            default:
                $list = 1;
                break;
        }
        $kline    = new KlineService();
        $kline    = $kline->loadKline($symbolcode,$list,'tf_futures_trade',500);
        $results  = [];

        foreach ($kline as $key => $item) {
            $item = json_decode($item);
            $data_items = [
                //$item->ctm * 1000,
                date($date, $item->ctm),
                $item->open * 1,
                $item->close * 1,
                $item->low * 1,
                $item->high * 1,
                $item->volume * 1,    //交易量
            ];

            array_unshift($results, $data_items);
        }
        $res = [
            'success' => TRUE,
            'symbol'  => $symbolcode,
            'data'    => ['lines' => $results],
        ];

        return ResultDataHelper::api(200, "kline data", $res);
    }

    /*
     * pc获取kline
     * */
    public function actionGetKline()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(403, "access done!");
        }
        $symbolcode = Yii::$app->request->get('symbol');
        $limit = Yii::$app->request->get('limit',500);

        if (empty($symbolcode)) {
            return ResultDataHelper::api(422, "产品code不能为空");
        }
        $type = Yii::$app->request->get('range');
        $date = 'Y-m-d H:i:s';
		// if( $type ){
			switch ($type) {
                case 'year'://周线
                    $list = 'year';
                    $date = 'Y';
                    break;
                case 'month'://周线
                    $list = 'month';
                    $date = 'Y-m';
                    break;
                case '604800000'://周线
                    $list = 'week';
                    $date = 'Y-m-d';
                    break;
                case '86400000'://日线
                    $list = 'day';
                    $date = 'Y-m-d';
                    break;
                case '14400000'://4小时
                    $list = 240;
                    $date = 'Y-m-d H';
                    break;
                case '3600000'://一小时
                    $list = 60;
                    break;
                case '1800000'://30分钟
                    $list = 30;
                    break;
                case '900000'://15分钟
                    $list = 15;
                    break;
                case '300000'://5分钟
                    $list = 5;
                    break;
                case '60000'://分时
                    $list = 1;
                    break;
                default:
                    $list = 1;
                    break;
	        }
	        // Yii::$app->services->kline->prex = 'rf_addon_tf_futures_symbol';
        	// $kline = Yii::$app->services->kline->getKline($symbolcode,$list);
	        $kline    = new KlineService();
            $kline    = $kline->loadKline($symbolcode,$list,'tf_futures_trade',$limit);
            
			$results                         = [];
	    
	        foreach ($kline as $key => $item) {
	            $item = json_decode($item);
	            $data_items = [
	                //$item->ctm * 1000,
	                date($date,$item->ctm),// * 1000,
	                $item->open * 1,
	                $item->high * 1,
	                $item->low * 1,
	                $item->close * 1,
	                $item->volume * 1,    //交易量
	            ];
	          
	            array_unshift($results, $data_items);
	        }
	        // array_unshift($results, $time);
	        // array_unshift($results, $times);
	        $res = [
	            'success' => TRUE,
	            'symbol'  => $symbolcode,
	            'data'    => ['lines' => $results],
	        ];
	
	        return ResultDataHelper::api(200, "kline data", $res);
		// }
		// return ResultDataHelper::api(200, "kline data",[]);
        
    }

    /**
     *  app获取分时线
     */
    public function actionGetLine()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(403, "access done!");
        }
        $symbolcode = Yii::$app->request->get('symbol');
        if (empty($symbolcode)) {
            return ResultDataHelper::api(422, "产品code不能为空");
        }
        $list                            = 1;
        $kline    = new KlineService();
        $newprice = $kline->loadTicekt($symbolcode,'tf_futures_trade');

        $kline    = $kline->loadKline($symbolcode,$list,'tf_futures_trade',500);
        $data                            = [];
        foreach ($kline as $key => $item) {
            $item = json_decode($item);
            $v1 = date('H:i', $item->ctm);
            $data[] = [
                $v1,
                $item->price * 1,
                $item->price * 1,
                $item->volume * 1,
            ];
        }
        //获取最新报价

        $res = [
            'success' => TRUE,
            'symbol'  => $symbolcode,
            'data'    => [$data,'yestclose'=>$newprice['close']],
        ];
        
        // $res = [
        //     'success' => TRUE,
        //     'symbol'  => $symbolcode,
        //     'data'    => ['lines' =>$results],
        // ];

        return ResultDataHelper::api(200, "kline data", $res);
    }

}